<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible"
  >
    <el-form
      :model="dataForm"
      :rules="dataRule"
      ref="dataForm"
      @keyup.enter.native="dataFormSubmit()"
      label-width="80px"
    >
      <el-form-item label="类型名称" prop="type">
        <el-input v-model="dataForm.type" placeholder="类型名称"></el-input>
      </el-form-item>
      <el-form-item label="备注" prop="comments">
        <el-input v-model="dataForm.comments" placeholder="备注"></el-input>
      </el-form-item>
      <el-form-item label="类型logo路径" prop="logoUrl">
        <single-upload v-model="dataForm.logoUrl"></single-upload>
      </el-form-item>
      <el-form-item label="删除标记" prop="delFlag">
        <el-radio v-model="dataForm.delFlag" label="0">不删除</el-radio>
           <el-radio v-model="dataForm.delFlag" label="1">删除</el-radio>
      </el-form-item>
      <el-form-item label="创建时间" prop="createTime">
        <div class="block">
          <el-date-picker
            v-model="dataForm.createTime"
            type="datetime"
            placeholder="选择日期时间"
          >
          </el-date-picker>
        </div>
        <!-- <el-input
          v-model="dataForm.createTime"
          placeholder="创建时间"
        ></el-input> -->
      </el-form-item>
      <el-form-item label="更新时间" prop="updateTime">

        <div class="block">
          <el-date-picker
            v-model="dataForm.updateTime"
            type="datetime"
            placeholder="选择日期时间"
          >
          </el-date-picker>
        </div>
        <!-- <el-input
          v-model="dataForm.updateTime"
          placeholder="更新时间"
        ></el-input> -->
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import SingleUpload from "../../common/singleUpload"; // 引入单文件上传组件
export default {
  components: {
    SingleUpload,
  },
  data() {
    return {
      visible: false,
      dataForm: {
        id: 0,
        type: "",
        comments: "",
        logoUrl: "",
        delFlag: "0",
        createTime: "",
        updateTime: "",
      },
      dataRule: {
        type: [
          { required: true, message: "类型名称不能为空", trigger: "blur" },
        ],
        comments: [
          { required: true, message: "备注不能为空", trigger: "blur" },
        ],
        logoUrl: [
          { required: true, message: "类型logo路径不能为空", trigger: "blur" },
        ],
        delFlag: [
          {
            required: true,
            message: "删除标记（0-正常，1-删除）不能为空",
            trigger: "blur",
          },
        ],
        createTime: [
          { required: false, message: "创建时间不能为空", trigger: "blur" },
        ],
        updateTime: [
          { required: false, message: "更新时间不能为空", trigger: "blur" },
        ],
      },
    };
  },
  methods: {
    init(id) {
      this.dataForm.id = id || 0;
      this.visible = true;
      this.$nextTick(() => {
        this.$refs["dataForm"].resetFields();
        if (this.dataForm.id) {
          this.$http({
            url: this.$http.adornUrl(`/question/type/info/${this.dataForm.id}`),
            method: "get",
            params: this.$http.adornParams(),
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.dataForm.type = data.type.type;
              this.dataForm.comments = data.type.comments;
              this.dataForm.logoUrl = data.type.logoUrl;
              this.dataForm.delFlag = data.type.delFlag + "";
              this.dataForm.createTime = data.type.createTime;
              this.dataForm.updateTime = data.type.updateTime;
            }
          });
        }
      });
    },
    // 表单提交
    dataFormSubmit() {
      this.$refs["dataForm"].validate((valid) => {
        if (valid) {
          this.$http({
            url: this.$http.adornUrl(
              `/question/type/${!this.dataForm.id ? "save" : "update"}`
            ),
            method: "post",
            data: this.$http.adornData({
              id: this.dataForm.id || undefined,
              type: this.dataForm.type,
              comments: this.dataForm.comments,
              logoUrl: this.dataForm.logoUrl,
              delFlag: this.dataForm.delFlag,
              createTime: this.dataForm.createTime,
              updateTime: this.dataForm.updateTime,
            }),
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.$message({
                message: "操作成功",
                type: "success",
                duration: 1500,
                onClose: () => {
                  this.visible = false;
                  this.$emit("refreshDataList");
                },
              });
            } else {
              this.$message.error(data.msg);
            }
          });
        }
      });
    },
  },
};
</script>
